package com.huxiaohui.ssbx.agent.supportopertionsapp;

import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.huxiaohui.ssbx.agent.BaseAgent;
import com.huxiaohui.ssbx.agent.prompt.PromptManagement;
import com.huxiaohui.ssbx.config.ModelConfig;
import com.huxiaohui.ssbx.constants.PromptConstants;
import com.huxiaohui.ssbx.constants.ResponseConstants;
import com.huxiaohui.ssbx.utils.ResponseFutureHolder;
import com.huxiaohui.ssbx.utils.SessionHolder;
import com.huxiaohui.ssbx.utils.SessionMessage;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.chat.model.ChatModel;

import java.util.concurrent.CompletableFuture;

@Slf4j
public class GetSupportOperationAppAgent extends BaseAgent {

    @Resource(name = ModelConfig.MAIN_AGENT)
    private ChatModel chatModel;
    @Resource
    private PromptManagement promptManagement;
    private final String sessionId;


    public GetSupportOperationAppAgent(String sessionId) {
        this.sessionId = sessionId;
    }

    @Override
    protected String solve(String task) {

        String requestId = IdUtil.fastSimpleUUID();
        // 创建CompletableFuture用于阻塞和接收结果
        CompletableFuture<String> future = new CompletableFuture<>();
        ResponseFutureHolder.put(requestId, future);

        try {
            // 通过session发送信息给客户端
            SessionMessage.sendMessage(SessionHolder.get(this.sessionId), requestId, ResponseConstants.GET_SUPPORT_OPERATION_APP_AGENT);
            return future.get();
        } catch (Exception e) {
            log.debug("处理GetSupportOperationAppAgent失败", e);
        } finally {
            ResponseFutureHolder.remove(requestId);
        }

        return "处理失败！";
    }

    @Override
    public ChatModel chatModel() {
        return this.chatModel;
    }

}
